﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_KQKT
{
    public partial class FormBaoCaoMau7 : MyFromBase
    {
        private PrepareDataForTreeView DataForTreeView = new PrepareDataForTreeView();
        private static int IDDonVi;
        private static int KyBaoCao;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckPermission();
            lblNotice.Visible = false;
            if (!IsPostBack)
            {
                BindDataToDropDownList(); ;
                DataForTreeView.PopulateRootLevel(trvDonVi);
            }
            KyBaoCao = Convert.ToInt32(ddlKyBaoCao.SelectedValue);
        }

        protected void CheckPermission()
        {
            Dictionary<string, int> Role = MyCommon.CheckPermission("BaoCaoMau7");
            if (Role == null || Role["Xem"] == 0)
            {
                Response.Redirect("Index.aspx?p=0");
            }
        }
        protected void trvDonVi_SelectedNodeChanged(object sender, EventArgs e)
        {
            lblDonVi.Text = "Đơn vị đã chọn : " + trvDonVi.SelectedNode.Text;
            IDDonVi = Convert.ToInt32(trvDonVi.SelectedValue);
        }

        protected void trvDonVi_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            DataForTreeView.PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);
        }

        protected void BindDataToDropDownList()
        {
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM='KyBaoCao'");
            ddlKyBaoCao.DataSource = dt;
            ddlKyBaoCao.DataTextField = "TenDM";
            ddlKyBaoCao.DataValueField = "ID";
            ddlKyBaoCao.DataBind();
        }

        protected void btnXemBaoCao_Click(object sender, EventArgs e)
        {
            if (trvDonVi.SelectedNode == null)
            {
                lblNotice.Visible = true;
                lblNotice.InnerText = "Vui lòng lựa chọn đơn vị muốn xem báo cáo";
            }
            else
            {
                List<SqlParameter> lstParams = new List<SqlParameter>();

                if (chkTongHop.Checked)
                {
                    lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                    lstParams.Add(SqlHelper.CreateParameterObject("@parentID", SqlDbType.NVarChar, ParameterDirection.Input, IDDonVi + ";"));
                    lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                    MyReportSQL = "SELECT A.TenDM,SUM(M1.F1)AS F1 ,SUM(M1.F2)AS F2 ,SUM(M1.F3)AS F3 ,SUM(M1.F4)AS F4 ,SUM(M1.F5)AS F5 ,SUM(M1.F6)AS F6 ,SUM(M1.F7)AS F7 ,SUM(M1.F8)AS F8 ,SUM(M1.F9)AS F9 ,SUM(M1.F10)AS F10 ,SUM(M1.F11)AS F11 ,SUM(M1.F12)AS F12 ,SUM(M1.F13)AS F13 ,SUM(M1.F14)AS F14 ,SUM(M1.F15)AS F15 ,SUM(M1.F16)AS F16 ,SUM(M1.F17)AS F17 ,SUM(M1.F18)AS F18 ,SUM(M1.F19)AS F19 ,SUM(M1.F20)AS F20 ,SUM(M1.F21)AS F21 ,SUM(M1.F22)AS F22 ,SUM(M1.F23) AS F23," +
                                  "SUM(M1.F24)AS F24 ,SUM(M1.F25)AS F25 ,SUM(M1.F26)AS F26 ,SUM(M1.F27)AS F27 ,SUM(M1.F28)AS F28 ,SUM(M1.F29)AS F29 ,SUM(M1.F30)AS F30 ,SUM(M1.F31)AS F31 ,SUM(M1.F32)AS F32 ,SUM(M1.F33) AS F33,A.BoiDen,A.LaToChuc,A.FatherID,A.STT " +
                                  "FROM dbo.tblBaoCaoMau7 M1 INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                                  "WHERE KyBaoCaoID_DM = @KyBaoCao AND DonViID IN (Select ID From tblDonVi Where parentID LIKE '%' + @parentID + '%') " +
                                  "GROUP BY A.TenDM,A.BoiDen,A.LaToChuc,A.STT,A.FatherID,A.ID " +
                                  "ORDER BY A.ID";
                }
                else
                {
                    lstParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.Int, ParameterDirection.Input, KyBaoCao));
                    lstParams.Add(SqlHelper.CreateParameterObject("@DonViID", SqlDbType.Int, ParameterDirection.Input, IDDonVi));
                    MyReportSQL = "SELECT M1.ID,A.TenDM,F1,F2,F3,F4,F5,F6,F7,F8,F9,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F22,F23,"+
                                  "F24,F25,F26,F27,F28,F29,F30,F31,F32,F33,A.BoiDen,A.LaToChuc,A.FatherID,A.STT " +
                                  "FROM dbo.tblBaoCaoMau7 M1 INNER JOIN dbo.tblDanhMucBaoCao A ON M1.TenBaoCaoID_DM = A.ID " +
                                  "WHERE KyBaoCaoID_DM = @KyBaoCao AND DonViID = @DonViID";

                }
                MyReportParams = lstParams;
                MyReportSQLParam = "SELECT BC.CM_SoBaoCao AS SoBaoCao ,BC.CM_TenBaoCao AS TenBaoCao,BC.CreateDate AS TuNgay,BC.UpdateDate AS DenNgay,BC.CM_NguoiLapBaoCao AS NguoiLapBaoCao,BC.CM_ThuTruong AS BKTDU,BC.CM_NgayBaoCao AS NgayBaoCao," +
                                   "DV.TenDV,BC.CM_1,BC.CM_2,BC.CM_3,BC.CM_4,BC.CM_5,BC.CM_6,BC.CM_7,BC.CM_8,BC.CM_9,BC.CM_10 FROM dbo.tblBaoCaoMau7 BC ,tblDonVi DV " +
                                   "WHERE BC.DonViID = DV.ID AND BC.KyBaoCaoID_DM = @KyBaoCao AND BC.DonViID = @DonViID AND BC.IsCommon = 1";
                string UnionParam = " UNION ALL SELECT '','',GETDATE(),GETDATE(),'','',GETDATE(),N'" + trvDonVi.SelectedNode.Text + "',0,0,0,'0',0,'0',0,'0',0,'0' WHERE NOT EXISTS (" + MyReportSQLParam + ")";
                MyReportSQLParam += UnionParam;
                MyReportName = "rpt07.rpt";
                MyTableName = "tblBaoCaoMau7";
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), null, "window.open('MyReportView.aspx');", true);
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("index.aspx");
        }
    }
}
